<?php
namespace Admin\Controller;
use Think\Controller;
class ProductController extends Controller {
	public function __construct(){
			parent::__construct();
			if(!$_SESSION['admin_is_login']){
				 $this->error('没有登录，请登录',U('admin/auth/login'));
			}
		}

    public function index(){

    		$product_model = M('product'); 
			$count = $product_model->count();
			$Page  = new \Org\Util\Page($count,10);
			$products = $product_model->field('*,product.id as id')->join('product_catalog ON product.cata_id = product_catalog.id','left')->Order('product.id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
			$this->assign('products',$products);
			$this->assign('page',$Page->show());
			$this->display(); 
		}
	public function add(){
		$this->assign('level1',M('product_catalog')->where('parent_id=0')->select());
		$this->display();
	}
	public function getCatalog($id){
		$id=I('get.id');
		$data=M('product_catalog')->where('parent_id='.$id)->select();
		$this->ajaxReturn($data);
	}

	public function do_add(){
		 $rules=array(
		     array('title','require','标题不能为空！'), 
		     array('cata_id','require','分类不能为空！'), 
		     array('img','require','图片不能为空！'), 
		     array('price','require','价格不能为空！'), 
		     array('stock','require','库存不能为空！'), 
		     array('desc','require','描述不能为空！'), 
		     array('content','require','内容不能为空！'), 
		     );

			$product_model = M("product"); // 实例化User对象
		if(!$product_model->validate($rules)->create()){
		     // 如果创建失败 表示验证没有通过 输出错误提示信息
		     $this->error($product_model->getError());
		 }else{
		 	M('product')->add(array(
		 			'title'=>I('post.title'),
		 			'cata_id'=>I('post.cata_id'),
		 			'img'=>I('post.img'),
		 			'price'=>I('post.price'),
		 			'stock'=>I('post.stock'),
		 			'desc'=>I('post.desc'),
		 			'content'=>I('post.content'),
		 		));
		 	$this->success('商品添加成功',U('admin/product/index'));

		 }
	}
	public function edit($id){
		$product=M('product')->where('id='.$id)->find();
		$level3=$product['cata_id'];
		$level3_catalog=M('product_catalog')->where('id='.$level3)->find();
		$level2=$level3_catalog['parent_id'];
		$level2_catalog=M('product_catalog')->where('id='.$level2)->find();
		$level1=$level2_catalog['parent_id'];
		$this->assign('level1',$level1);
		$this->assign('level2',$level2);
		$this->assign('level3',$level3);
		$this->assign('product',$product);
		$this->assign('level1_data',M('product_catalog')->where('parent_id=0')->select());
		$this->display();
	}
	
	public function do_edit(){
		 $rules=array(
		     array('title','require','标题不能为空！'), 
		     array('cata_id','require','分类不能为空！'), 
		     array('img','require','图片不能为空！'), 
		     array('price','require','价格不能为空！'), 
		     array('stock','require','库存不能为空！'), 
		     array('desc','require','描述不能为空！'), 
		     array('content','require','内容不能为空！'), 
		     );

			$product_model = M("product"); // 实例化User对象
		if(!$product_model->validate($rules)->create()){
		     // 如果创建失败 表示验证没有通过 输出错误提示信息
		     $this->error($product_model->getError());
		 }else{
		 	M('product')->where('id='.I('post.id'))->save(array(
		 			'title'=>I('post.title'),
		 			'cata_id'=>I('post.cata_id'),
		 			'img'=>I('post.img'),
		 			'price'=>I('post.price'),
		 			'stock'=>I('post.stock'),
		 			'desc'=>I('post.desc'),
		 			'content'=>I('post.content'),
		 		));
		 	$this->success('商品编辑成功',U('admin/product/index'));

		 }
	}
	public function delete($id){
		M('product')->where('id='.$id)->delete();

		$this->success('商品删除成功',U('admin/product/index'));
	}
	public function ban($id){
		M('product')->where('id='.$id)->save(array(
				'status'=>'0'
			));
		$this->success('下架成功',U('admin/product/index'));
	}
	public function enable($id){
		M('product')->where('id='.$id)->save(array(
				'status'=>'1'
			));
		$this->success('上架成功',U('admin/product/index'));
	}

}